package com.google.webCash.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Timer;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Margins;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Viewport;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;
import com.gwtext.client.widgets.layout.ColumnLayout;
import com.gwtext.client.widgets.layout.ColumnLayoutData;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.tree.TreeNode;
import com.gwtext.client.widgets.tree.TreePanel;
import com.gwtext.client.widgets.tree.event.TreePanelListenerAdapter;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class WebCash implements EntryPoint {

//	private static final String SERVER_ERROR = "An error occurred while "
//			+ "attempting to contact the server. Please check your network "
//			+ "connection and try again.";
//
//	private final GreetingServiceAsync greetingService = GWT
//			.create(GreetingService.class);

	int index = 100;
	
	Timer resh;
	
	private MainPanel main = MainPanel.getInstacne();
	private MessageConsole messageConsole = MessageConsole.getInstacne();
	
	/**
	 * This is the entry point method.
	 * @wbp.parser.entryPoint
	 */
	public void onModuleLoad() {
		Panel panel = new Panel();
		panel.setPaddings(10);
		panel.setLayout(new FitLayout());

		Panel bodyPanel = creatBody();

		panel.add(bodyPanel);
		
		Viewport view = new Viewport(panel);
		view.doLayout();
	}
	
	private Panel creatBody(){
		Panel borderPanel = new Panel();
//		borderPanel.setBorder(false);
		borderPanel.setLayout(new BorderLayout());
		
		Panel blankPanel = new Panel();
		blankPanel.setBorder(false);
		blankPanel.setHeight(5);
		blankPanel.setBodyStyle("background-color:#DFE8F6");
		
		Panel linePanel = new Panel();
		linePanel.setBorder(false);
		linePanel.setHeight(1);
		linePanel.setBodyStyle("background-color:#99BBE8");
		
		Panel titlePanel = creatTitle(blankPanel, linePanel);

		BorderLayoutData titleData = new BorderLayoutData(RegionPosition.NORTH);
		titleData.setAutoHide(false);
		borderPanel.add(titlePanel, titleData);

		Panel bottomPanel = creatBottom(blankPanel, linePanel);
		
		BorderLayoutData bottomData = new BorderLayoutData(RegionPosition.SOUTH);
		bottomData.setMinSize(100);
		bottomData.setMaxSize(200);
		bottomData.setMargins(new Margins(0, 0, 0, 0));
		borderPanel.add(bottomPanel, bottomData);
		
		Panel menuPanel = creatMenu();

		BorderLayoutData menuData = new BorderLayoutData(RegionPosition.WEST);
		menuData.setSplit(true);
		menuData.setMinSize(175);
		menuData.setMaxSize(400);
		menuData.setMargins(new Margins(0, 5, 0, 0));

		borderPanel.add(menuPanel, menuData);

		BorderLayoutData mainData = new BorderLayoutData(RegionPosition.CENTER);
		mainData.setMargins(new Margins(0, 0, 5, 0));
		
		borderPanel.add(main, mainData);
		return borderPanel;
	}
	
	private Panel creatTitle(Panel blankPanel, Panel linePanel){
		Panel title = new Panel();
		title.setBorder(false);
		title.setHeight(57);
//		northPanel.setBodyStyle("background-color:#FFFF88");

		Panel borderPanel = new Panel();
		borderPanel.setBorder(false);
		borderPanel.setHeight(47);
		borderPanel.setLayout(new ColumnLayout());
		borderPanel.setStyle("vertical-align: middle");
		
		Panel logoPanel = new Panel();
		logoPanel.setBorder(false);
		logoPanel.setHtml("<img src=\"webcash\\gwtLogo.jpg\" width=240 height=47>");
//		logoPanel.setHeight(47);
		logoPanel.setWidth(240);
		borderPanel.add(logoPanel);
		
		Panel midPanel = new Panel();
		midPanel.setBorder(false);
		midPanel.setHtml("<br><center><h2>欢迎使用成都中联信通科技有限公司产品</h2></center>");
		borderPanel.add(midPanel, new ColumnLayoutData(.8));
		
		borderPanel.add(messageConsole, new ColumnLayoutData(.2));
		
//		titlePanel.setBodyStyle("background-color:#FFFF88");
		title.add(borderPanel, new BorderLayoutData(RegionPosition.NORTH));
		title.add(linePanel, new BorderLayoutData(RegionPosition.CENTER));
		title.add(blankPanel, new BorderLayoutData(RegionPosition.SOUTH));
		
		return title;
	}
	
	private Panel creatBottom(Panel blankPanel, Panel linePanel){
		Panel bottom = new Panel();
		bottom.setBorder(false);
		bottom.setHeight(106);
		
		Panel copyPanel = new Panel();
		copyPanel.setBorder(false);
		copyPanel.setHtml("<p>copyright 2009-2010</p>");
		copyPanel.setHeight(100);
		copyPanel.setBodyStyle("background-color:#CDEB8B; text-align: center;");
		
		bottom.add(blankPanel, new BorderLayoutData(RegionPosition.NORTH));
		bottom.add(linePanel, new BorderLayoutData(RegionPosition.CENTER));
		bottom.add(copyPanel, new BorderLayoutData(RegionPosition.SOUTH));
		
		return bottom;
	}
	
	private Panel creatMenu(){
		Panel menu = new Panel();
//		menu.setHtml("<p>west panel</p>");
		menu.setTitle("导航");
//		menu.setBodyStyle("background-color:#EEEEEE");
		menu.setCollapsible(true);
		menu.setWidth(200);
        
		TreePanel tree = new TreePanel();
		tree.setBorder(false);
		tree.setHeight("100%");
		
		TreeNode root = new TreeNode();
		
		TreeNode menu0 = new TreeNode();
		menu0.setText("欢迎");
		menu0.setId("0");
		
		TreeNode menu1 = new TreeNode();
		menu1.setText("监控");
		menu1.setId("1");
		
		TreeNode menu2 = new TreeNode();
		menu2.setText("地图");
		menu2.setId("2");
		
		root.appendChild(menu0);
		root.appendChild(menu1);
		root.appendChild(menu2);
		root.setExpandable(true);
		
		tree.setRootNode(root);
		tree.setRootVisible(false);
		
		menu.add(tree);
		
		tree.addListener(new TreePanelListenerAdapter(){

			@Override
			public void onClick(TreeNode node, EventObject e) {
				String id = node.getId();
				if(id.equals("0")){
					main.show0();
				} else if(id.equals("1")){
					main.show1();
				} else if(id.equals("2")){
					main.show2();
				}
			}
			
		});
		
		return menu;
	}
}
